<template>
    <div class="role-menu">
        <el-container>
            <el-main>
                <div>
                    <el-transfer
                            filterable
                            filter-placeholder="输入名称进行搜索"
                            v-model="value"
                            :data="data"
                            :titles="['可选菜单', '已关联']"
                            :button-texts="['移除', '关联']"
                            @change="transferChange()"
                    ></el-transfer>
                </div>

            </el-main>

            <el-aside width="100px"
                      style="margin-right: 0px;height:auto;background-color:#fff;border: 1px solid #f5f0f0">
                <div
                        v-for="(r,index) in roles"
                        :key="r.id"
                        :class="chosenIndex==index?'chosen':'unchosen'"
                        @click="changeRole(r,index)">{{r.name}}
                </div>
            </el-aside>
        </el-container>

    </div>
</template>

<script>
    import {allRoleList, relevancyMenu} from "@/api/roleApi";
    import {getMenuList, getMenuByRole} from "@/api/menuApi";

    export default {
        name: "roleMenu",
        data() {
            return {
                chosenIndex: 0,
                roles: [],
                data: [],
                value: [],
            }
        },
        created() {
            this.searchRoleLists();
            // for (let i = 1; i <= 15; i++) {
            //     this.data.push({
            //         key: i+'a',
            //         label: `备选项 ${ i }`,
            //         disabled: i % 4 === 0
            //     });
            // }
        },
        methods: {
            //查询角色列表
            searchRoleLists() {
                allRoleList(this.searchParam).then(res => {
                    this.roles = res;
                    this.searchMenuList();
                    if (res.length > 0) {
                        this.searchMenuByRoleId(res[0].id);
                    }
                })
            },
            //查询所有菜单
            searchMenuList() {
                getMenuList(this.searchParam).then(res => {
                    res.forEach(item => {
                        let d = {
                            key: item.id,
                            label: item.name
                        }
                        this.data.push(d);
                    });
                })
            },
            //通过角色id查询查单
            searchMenuByRoleId(roleId) {
                getMenuByRole({id: roleId}).then(res => {
                    this.value = [];
                    res.forEach(item => {
                        this.value.push(item.id);
                    })
                })
            },
            //切换change
            transferChange() {
                let param = {
                    roleId: this.roles[this.chosenIndex].id,
                    menuIds: this.value
                }
                relevancyMenu(param).then(res => {
                    this.$message.success('操作成功');
                })

            },
            changeRole(item, index) {
                this.chosenIndex = index;
                this.searchMenuByRoleId(item.id);
            },
        }
    }
</script>

<style scoped lang="scss">
    .role-menu {

        .chosen {
            cursor: pointer;
            color: white;
            font-size: small;
            text-align: center;
            background-color: #409EFF;
            padding: 10px 0;
            margin: 2px 0;
        }

        .unchosen {
            cursor: pointer;
            font-size: small;
            color: black;
            text-align: center;
            padding: 10px 0;
            margin: 2px 0;
            font-size: small;
        }

        .unchosen:hover {
            background-color: rgba(64, 158, 255, 0.5);
        }

        .el-transfer {
            margin-left: 30%;
        }


    }

</style>